package com.shanghai.shanghaielectric.UI.refreshView;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shanghai.shanghaielectric.Model.db.ErrorInfoModel;
import com.shanghai.shanghaielectric.MyApplication;
import com.shanghai.shanghaielectric.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by wnw on 16-5-26.
 */
public class MyAdapter_errorInfo extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    //定义一个集合，接收从Activity中传递过来的数据和上下文
    private List<ErrorInfoModel> mList;
    private Context mContext;

    public MyAdapter_errorInfo(Context context, List<ErrorInfoModel> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(mContext).inflate(R.layout.warning_item, parent, false);
        return new MyHolder(layout);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyHolder) {
            long id = mList.get(position).getId();
            int unit_id = mList.get(position).getUnit_ID();
            String errorContent = mList.get(position).getErrorContent();
            Date item_startTime =new Date( Long.parseLong(mList.get(position).getErrorDate()));

            if (mList.get(position).isClear()==1){
                ((MyHolder) holder).item_id.setText(position+1+"");
                ((MyHolder) holder).item_id.setTextColor(MyApplication.getContext().getResources().getColor(R.color.green_color));

                ((MyHolder) holder).item_unit_num.setText(unit_id+"");
                ((MyHolder) holder).item_unit_num.setTextColor(MyApplication.getContext().getResources().getColor(R.color.green_color));

                ((MyHolder) holder).item_errorName.setText(errorContent);
                ((MyHolder) holder).item_errorName.setTextColor(MyApplication.getContext().getResources().getColor(R.color.green_color));

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");

                ((MyHolder) holder).item_startTime.setText(formatter.format(item_startTime));
                ((MyHolder) holder).item_startTime.setTextColor(MyApplication.getContext().getResources().getColor(R.color.green_color));

            }else {

                ((MyHolder) holder).item_id.setText(position+1+"");
                ((MyHolder) holder).item_id.setTextColor(MyApplication.getContext().getResources().getColor(R.color.red_color));

                ((MyHolder) holder).item_unit_num.setText(unit_id+"");
                ((MyHolder) holder).item_unit_num.setTextColor(MyApplication.getContext().getResources().getColor(R.color.red_color));

                ((MyHolder) holder).item_errorName.setText(errorContent);
                ((MyHolder) holder).item_errorName.setTextColor(MyApplication.getContext().getResources().getColor(R.color.red_color));

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");

                ((MyHolder) holder).item_startTime.setText(formatter.format(item_startTime));
                ((MyHolder) holder).item_startTime.setTextColor(MyApplication.getContext().getResources().getColor(R.color.red_color));

            }

        }
    }

    class MyHolder extends RecyclerView.ViewHolder {

        TextView item_id, item_unit_num, item_errorName, item_startTime;

        public MyHolder(View itemView) {
            super(itemView);
            item_id = (TextView) itemView.findViewById(R.id.item_id);
            item_unit_num = (TextView) itemView.findViewById(R.id.item_unit_num);
            item_errorName = (TextView) itemView.findViewById(R.id.item_errorName);
            item_startTime = (TextView) itemView.findViewById(R.id.item_startTime);
        }
    }

//    public void setData(ErrorInfoModel errorInfoModel){
//        mList.add(0,errorInfoModel);
//    }
}
